package com.frinder.quartz;

import com.frinder.jobs.Job3;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Created by kiktech on 2017/6/2.
 */
@EnableScheduling
@Configuration
public class MyQuartzConfiguration {


    @Bean
    public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
        MethodInvokingJobDetailFactoryBean jobDetailFactoryBean = new MethodInvokingJobDetailFactoryBean();
        jobDetailFactoryBean.setTargetObject(new Job3());
        jobDetailFactoryBean.setTargetMethod("handle");
        jobDetailFactoryBean.setConcurrent(false);
        return jobDetailFactoryBean;
    }


    @Bean
    public CronTriggerFactoryBean cronTriggerBean(JobDetail jobDetail) {
        CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
        cronTriggerFactoryBean.setCronExpression("0/3 * * * * ?");
        cronTriggerFactoryBean.setJobDetail(jobDetail);
        return cronTriggerFactoryBean;
    }


    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(CronTrigger cronTrigger) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setStartupDelay(10);
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setTriggers(cronTrigger);
        return schedulerFactoryBean;
    }

}
